package com.examplemartin.jpademo.controller.sell;


import com.examplemartin.jpademo.bean.AjaxResult;
import com.examplemartin.jpademo.pojo.po.UserPO;
import com.examplemartin.jpademo.pojo.po.cust.CustPO;
import com.examplemartin.jpademo.pojo.po.good.GoodPO;
import com.examplemartin.jpademo.pojo.po.good.GoodUnit;
import com.examplemartin.jpademo.pojo.po.repository.sell.SellOutPO;
import com.examplemartin.jpademo.pojo.po.sell.SellDetailPO;
import com.examplemartin.jpademo.pojo.po.sell.SellPO;
import com.examplemartin.jpademo.pojo.po.stock.SummGoodPO;
import com.examplemartin.jpademo.pojo.vo.SellVO;
import com.examplemartin.jpademo.pojo.vo.purchase.RepoinfoVO;
import com.examplemartin.jpademo.service.CustService;
import com.examplemartin.jpademo.service.GoodService;
import com.examplemartin.jpademo.service.RepoInfoService;
import com.examplemartin.jpademo.service.pur.PurchaseService;
import com.examplemartin.jpademo.service.sell.SellDetailService;
import com.examplemartin.jpademo.service.sell.SellService;
import com.examplemartin.jpademo.service.sellout.SellOutService;
import com.examplemartin.jpademo.service.stock.SummGoodService;
import com.examplemartin.jpademo.util.SessionUtil;
import com.examplemartin.jpademo.util.UUIDUtile;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@RequestMapping("/sell")
@Controller
@Log4j2
public class SellController {

    @Resource
    private SellService sellService;
    @Resource
    private SellDetailService sellDetailService;

    @Resource
    private CustService custService;
    @Resource
    private GoodService goodService;

    @Resource
    private RepoInfoService repoInfoService;


    /**
     * 跳转到 销售订单页面
     *
     * @param model
     *
     * @return
     */
    @GetMapping("/selllist.do")
    public String sellListDo(Model model) {
        model.addAttribute("title", "销售订单列表");

        // 销售订单状态
        model.addAttribute("sellstatus", SellService.SELL_STATUS_MAP);

        // 生成vo
        List<SellPO> all = sellService.findAll();
        ArrayList<SellVO> sellVOS = new ArrayList<>();
        for (SellPO sellPO : all) {
            SellVO vo = new SellVO();
            vo.setComment(sellPO.getComment());
            vo.setCreateTime(sellPO.getCreateTime());
            vo.setUpdateTime(sellPO.getUpdateTime());
            vo.setUserPO(sellPO.getUserPO());
            vo.setId(sellPO.getId());
            vo.setName(sellPO.getName());
            vo.setNeed(sellPO.getNeed());
            vo.setStatus(sellPO.getStatus());
            vo.setCustPO(sellPO.getCustPO());
            // 合计总价
            Float total = 0f;
            List<SellDetailPO> sellDetailPOList = sellPO.getSellDetailPOList();
            for (SellDetailPO sellDetailPO : sellDetailPOList) {
                total = total + sellDetailPO.getPrice() * sellDetailPO.getGoodcount();
            }
            vo.setSumSellDetail(total);
            sellVOS.add(vo);
        }

        model.addAttribute("selllist", sellVOS);

        return "sell/selllist";
    }

    @Resource
    private SummGoodService summGoodService;
    /**
     * 跳转到 添加销售单页面
     *
     * @param model
     *
     * @return
     */
    @GetMapping("addsell.do")
    public String addSellDo(Model model) {
        model.addAttribute("title", "新增订单");
        model.addAttribute("sellno", UUIDUtile.engender("SELL"));
        model.addAttribute("ctime", new Date());

        // model  -- 计量单位
        model.addAttribute("goodunitname", GoodUnit.UNIT_NAME);
        model.addAttribute("goodunitval", GoodUnit.UNIT_VALUE);

        // 库存
//        List<RepoinfoVO> allGroupGoodId = repoInfoService.findAllGroupByGoodId();
        List<SummGoodPO> allsummGood = summGoodService.findAll();

        model.addAttribute("repolist", allsummGood);


        // 客户列表 -- 非禁用客户
        List<CustPO> allByStatus = custService.findAllByStatus(CustService.CUST_STATUS_ON);
        model.addAttribute("custlist", allByStatus);
        return "sell/addsell";
    }

    /**
     * /sell/getGoodlist.ajax
     * 库存内所有商品 ,不同库存合并处理,出数量
     *
     * @return
     */
    @PostMapping("/getGoodlist.ajax")
    @ResponseBody
    public AjaxResult getGoodlistAjax() {
//        List<RepoinfoVO> allGroupByGoodId = repoInfoService.findAllGroupByGoodId();
        List<SummGoodPO> allGroupByGoodId = summGoodService.findAll();
        HashMap<String, List<SummGoodPO>> stringListHashMap = new HashMap<>();
        stringListHashMap.put("goodlist", allGroupByGoodId);

        return AjaxResult.success(200, "", stringListHashMap);
    }

    /**
     * 生成新建  销售订单
     *
     * @param sellPO
     *
     * @return
     */
    @PostMapping("/savesellPO.ajax")
    @ResponseBody
    public AjaxResult savesellPOAjax(@RequestBody SellPO sellPO) {

        // 库存信息
//        List<RepoinfoVO> allGroupByGoodId = repoInfoService.findAllGroupByGoodId();
        List<SummGoodPO> allGroupByGoodId = summGoodService.findAll();
        //责任人
        UserPO loninUser = SessionUtil.getLoninUser();

/*//        检查是否缺货
        Boolean LESSGOOD = false;
        List<SellDetailPO> sellDetailPOList = sellPO.getSellDetailPOList();
        // 循环判断是否出现缺货
        for (SellDetailPO sellDetailPO : sellDetailPOList) {
            GoodPO goodPO = sellDetailPO.getGoodPO();
            Integer goodcount = sellDetailPO.getGoodcount();
            for (RepoinfoVO repoinfoVO : allGroupByGoodId) {
                GoodPO goodPO1 = repoinfoVO.getGoodPO();
                Integer goodcount1 = repoinfoVO.getGoodcount();
                if(goodPO1.getId().longValue() == goodPO.getId().longValue()){
                    if(goodcount1<goodcount){
                        LESSGOOD = true;
                    }
                    break;
                }

            }
            if(LESSGOOD == true){
                break;
            }

        }



        SellPO newSellPO = new SellPO();
        // 销售订单号
        newSellPO.setName(sellPO.getName());
        // 销售订单日期
        newSellPO.setCreateTime(new Date());
        // 销售订单顾客
        newSellPO.setCustPO(sellPO.getCustPO());
        // 销售订单注释
        newSellPO.setComment(sellPO.getComment());
        // 销售订单责任人
        newSellPO.setUserPO(loninUser);
        // 销售订单need日期
        newSellPO.setNeed(sellPO.getNeed());
        // 订单状态 -- 缺货 , 待审
        if(LESSGOOD){
            newSellPO.setStatus(SellService.SELL_STATUS_LESSGOOG);
        }else{
            newSellPO.setStatus(SellService.SELL_STATUS_SPARE);
        }

        // 生成订单
        newSellPO = sellService.save(newSellPO);
        // 保存订单

        // 生成明细
        newSellPO.setSellDetailPOList(new ArrayList<SellDetailPO>());
        for (SellDetailPO sellDetailPO : sellPO.getSellDetailPOList()) {
            SellDetailPO newDetail = new SellDetailPO();
            newDetail.setSellPO(newSellPO);

            GoodPO byIdgood = goodService.findById(sellDetailPO.getGoodPO().getId());
            newDetail.setGoodPO(byIdgood);


            newDetail.setPrice(sellDetailPO.getPrice());
            newDetail.setGoodcount(sellDetailPO.getGoodcount());
            // 保存明细
            SellDetailPO save = sellDetailService.save(newDetail);
            newSellPO.getSellDetailPOList().add(save);

        }
        // 再次保存订单
        sellService.save(newSellPO);*/
        AjaxResult ajaxResult = sellService.savesellPOAjax(sellPO, loninUser);
        return ajaxResult;
    }

    /**
     * 订单 修订
     *
     * @param sellid
     *
     * @return
     */
    @GetMapping("/editsell.do")
    public String editsellDo(Long sellid, Model model) {
        model.addAttribute("title", "采购订单修订");
// --------
        // model  -- 计量单位
        model.addAttribute("goodunitname", GoodUnit.UNIT_NAME);
        model.addAttribute("goodunitval", GoodUnit.UNIT_VALUE);

        // model - sell 订单
        SellPO byIdsell = sellService.findById(sellid);
        model.addAttribute("sell", byIdsell);

        // model - goodlist 商品下拉菜单
//        List<RepoinfoVO> allGroupByGoodId = repoInfoService.findAllGroupByGoodId();
        List<SummGoodPO> allGroupByGoodId = summGoodService.findAll();

        // 1 去除 已有商品选项
        ArrayList<GoodPO> goodPOS = new ArrayList<>();

//        for (SellDetailPO sellDetailPO : byIdsell.getSellDetailPOList()) {
//            goodPOS.add(sellDetailPO.getGoodPO());
//        }

// 库存
//        List<RepoinfoVO> allGroupGoodId = repoInfoService.findAllGroupByGoodId();
        List<SummGoodPO> allGroupGoodId = summGoodService.findAll();
        HashMap<Long, Integer> repomap = new HashMap<>();
        for (SummGoodPO summGoodPO : allGroupByGoodId) {
            repomap.put(summGoodPO.getGoodPO().getId(),summGoodPO.getGoodcount());
        }
        model.addAttribute("repomap", repomap);

        boolean flag = false;

        for (SummGoodPO summGoodPO : allGroupByGoodId) {
            flag = false;
            Long goodinrepo = summGoodPO.getGoodPO().getId();
            List<SellDetailPO> sellDetailPOList = byIdsell.getSellDetailPOList();
            for (SellDetailPO sellDetailPO : sellDetailPOList) {
                if(sellDetailPO.getGoodPO().getId().longValue() ==goodinrepo.longValue()){
                    flag=true;
                    break;
                }
            }
            if(!flag){
                goodPOS.add(summGoodPO.getGoodPO());
            }
        }

        model.addAttribute("goodlist", goodPOS);
        model.addAttribute("allgoodlist", allGroupByGoodId);

        model.addAttribute("sell_status_map", SellService.SELL_STATUS_MAP);
        // model - 订单状态栏
        model.addAttribute("purstatusname", PurchaseService.PURPO_STATUS_NAME);
        model.addAttribute("purstatusval", PurchaseService.PURPO_STATUS_VALUE);
//        ----------


        return "sell/editsell";
    }

    /**
     * 保存修改的销售订单
     * @param sellPO
     * @return
     */
    @PostMapping("/savemodifysellPO.ajax")
    @ResponseBody
    public AjaxResult savemodifysellPOAjax(@RequestBody SellPO sellPO){

        return sellService.savemodifysellPOAjax(sellPO);
    }

    /**
     * 销售订单审核
     * @param model
     * @return
     */
    @GetMapping("/auditselllist.do")
    public String auditsellDo(Model model){
        model.addAttribute("title","销售订单审核");

        // model  -- 计量单位
        model.addAttribute("goodunitname", GoodUnit.UNIT_NAME);
        model.addAttribute("goodunitval", GoodUnit.UNIT_VALUE);

// 生成订单列表
        // 0 编制状态
        ArrayList<SellVO> sparesellVOS = new ArrayList<>();
        List<SellPO> allByStatusOOrderByNeed = sellService.findAllByStatusOOrderByNeed(SellService.SELL_STATUS_SPARE);
        for (SellPO sellPO : allByStatusOOrderByNeed) {
            SellVO vo = new SellVO();
            vo.setId(sellPO.getId());
            vo.setCustPO(sellPO.getCustPO());
            vo.setCreateTime(sellPO.getCreateTime());
            vo.setStatus(sellPO.getStatus());
            vo.setUserPO(sellPO.getUserPO());
            vo.setName(sellPO.getName());
            vo.setNeed(sellPO.getNeed());
            vo.setComment(sellPO.getComment());

            // 总计金额
            Float total = 0f;
            for (SellDetailPO sellDetailPO : sellPO.getSellDetailPOList()) {
                total = total +sellDetailPO.getPrice()*sellDetailPO.getGoodcount();
            }
            vo.setSumSellDetail(total);
            sparesellVOS.add(vo);
        }


        // 1 缺货状态
        ArrayList<SellVO> lesssellVOS = new ArrayList<>();
        allByStatusOOrderByNeed = sellService.findAllByStatusOOrderByNeed(SellService.SELL_STATUS_LESSGOOG);
        for (SellPO sellPO : allByStatusOOrderByNeed) {
            SellVO vo = new SellVO();
            vo.setId(sellPO.getId());
            vo.setCustPO(sellPO.getCustPO());
            vo.setCreateTime(sellPO.getCreateTime());
            vo.setStatus(sellPO.getStatus());
            vo.setUserPO(sellPO.getUserPO());
            vo.setName(sellPO.getName());
            vo.setNeed(sellPO.getNeed());
            vo.setComment(sellPO.getComment());
            // 总计金额
            Float total = 0f;
            for (SellDetailPO sellDetailPO : sellPO.getSellDetailPOList()) {
                total = total +sellDetailPO.getPrice()*sellDetailPO.getGoodcount();
            }
            vo.setSumSellDetail(total);
            lesssellVOS.add(vo);
        }


        // 2 进行状态
        ArrayList<SellVO> runsellVOS = new ArrayList<>();
        allByStatusOOrderByNeed = sellService.findAllByStatusOOrderByNeed(SellService.SELL_STATUS_RUNNING);
        for (SellPO sellPO : allByStatusOOrderByNeed) {
            SellVO vo = new SellVO();
            vo.setId(sellPO.getId());
            vo.setCustPO(sellPO.getCustPO());
            vo.setCreateTime(sellPO.getCreateTime());
            vo.setStatus(sellPO.getStatus());
            vo.setUserPO(sellPO.getUserPO());
            vo.setName(sellPO.getName());
            vo.setNeed(sellPO.getNeed());
            vo.setComment(sellPO.getComment());
            // 总计金额
            Float total = 0f;
            for (SellDetailPO sellDetailPO : sellPO.getSellDetailPOList()) {
                total = total +sellDetailPO.getPrice()*sellDetailPO.getGoodcount();
            }
            vo.setSumSellDetail(total);
            runsellVOS.add(vo);
        }


        // 3 进行状态
        ArrayList<SellVO> finishsellVOS = new ArrayList<>();
        allByStatusOOrderByNeed = sellService.findAllByStatusOOrderByNeed(SellService.SELL_STATUS_FINISH);
        for (SellPO sellPO : allByStatusOOrderByNeed) {
            SellVO vo = new SellVO();
            vo.setId(sellPO.getId());
            vo.setCustPO(sellPO.getCustPO());
            vo.setCreateTime(sellPO.getCreateTime());
            vo.setStatus(sellPO.getStatus());
            vo.setUserPO(sellPO.getUserPO());
            vo.setName(sellPO.getName());
            vo.setNeed(sellPO.getNeed());
            vo.setComment(sellPO.getComment());
            // 总计金额
            Float total = 0f;
            for (SellDetailPO sellDetailPO : sellPO.getSellDetailPOList()) {
                total = total +sellDetailPO.getPrice()*sellDetailPO.getGoodcount();
            }
            vo.setSumSellDetail(total);
            finishsellVOS.add(vo);
        }

        ArrayList<SellVO> all = new ArrayList<SellVO>();
        all.addAll(sparesellVOS);
        all.addAll(lesssellVOS);
        all.addAll(runsellVOS);
        all.addAll(finishsellVOS);
        model.addAttribute("selllist", all);
        model.addAttribute("sellstatusval",SellService.SELL_STATUS_MAP);
        model.addAttribute("sell_status_map", SellService.SELL_STATUS_MAP);
        return "sell/auditsell";
    }

    @Resource
    private SellOutService sellOutService;

    /**
     * auditlist.do
     * 跳转到审核订单页面
     */
    @GetMapping("/auditlist.do")
    public String auditselldetailDo(Long sellid, Model model) {
        model.addAttribute("title", "订单审核");

        // model - goodlist 商品下拉菜单
//        List<RepoinfoVO> allGroupByGoodId = repoInfoService.findAllGroupByGoodId();
        List<SummGoodPO> allGroupByGoodId = summGoodService.findAll();
        List<RepoinfoVO> allGroupGoodId = repoInfoService.findAllGroupByGoodId();
        HashMap<Long, Integer> repomap = new HashMap<>();
        for (SummGoodPO summGoodPO : allGroupByGoodId) {
            repomap.put(summGoodPO.getGoodPO().getId(),summGoodPO.getGoodcount());
        }
        model.addAttribute("repomap", repomap);

        // model - pur 订单
        SellPO byIdsell = sellService.findById(sellid);
        model.addAttribute("sell", byIdsell);

        // model - goodlist 商品下拉菜单

        // 1 去除 已有商品选项
        ArrayList<GoodPO> goodPOS = new ArrayList<>();



        model.addAttribute("goodlist", allGroupByGoodId);

        // model - 订单状态栏
        HashMap<Integer, String> statusmap = new HashMap<>();
        statusmap.put(SellService.SELL_STATUS_SPARE, SellService.SELL_STATUS_SPARE_NAME);
        statusmap.put(SellService.SELL_STATUS_RUNNING, SellService.SELL_STATUS_RUNING_NAME);
        statusmap.put(SellService.SELL_STATUS_FINISH, SellService.SELL_STATUS_FINISH_NAME);
        statusmap.put(SellService.SELL_STATUS_LOCK, SellService.SELL_STATUS_LOCK_NAME);


        model.addAttribute("sell_status_map", statusmap);

        // 查询已出库库明细数量 -- 有多少明细单  -- 注 map多次转换 好像有问题

        Map<String, Object> getselloutlist = sellService.getSellOutList(sellid);

        List<SellOutPO> sellOutList = sellOutService.findAllBySellPO(byIdsell);

        model.addAttribute("allselloutlist",sellOutList); // 所有入库单
        model.addAttribute("goodmap",getselloutlist.get("goodmap")); // 所有入库单



        return "sell/auditselldetail";
    }


    /**
     * 把订单 转换为 .... 状态
     * /purchase/auditPurPO.ajax
     *
     * @param sellPO
     *
     * @return
     */
    @PostMapping("/auditsellPO.ajax")
    @ResponseBody
    public AjaxResult auditPurPOAjax(@RequestBody SellPO sellPO) {

        AjaxResult ajaxResult =sellService.auditSellPOAjax(sellPO);

        return ajaxResult;
    }

}
